<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*业务接口表api*/
class InterfaceApi extends  BaseApi
{
	/**
	 * 测试地址
	 * http://tp5.com/public/api/InterfaceApi/add/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c
	 * */
	 public function _initialize()
    {
          parent::_initialize();
    }
	/**
	 * 业务接口插入
	 * http://tp5.com/public/api/InterfaceApi/add/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c
	 * */
	public  function add(){
		
		$data=input('');
		if(!empty($data['card_no'])){
			$data['card_no1']=str_pad($data['card_no'], 10,'0',STR_PAD_LEFT);
			$data['card_no']=str_pad($data['card_no'], 10,'0',STR_PAD_LEFT);
		}
		$data['card_no']=$this->to_hex_revers($data['card_no']);
		//新增卡号
		$flag=$this->validate($data,'card');
		if($flag!==true){//参数验证错误
			return $this->ajax_error($flag,"0003",FALSE);
		}
		//新增会员
		$flag=$this->validate($data,'InterfaceApi.add');
		if($flag!==TRUE){
			return $this->ajax_error($flag,'0003',FALSE);
		}else{
			model('Card')->create($data,TRUE);
			model('InterfaceApi')->create($data,TRUE);
			$card_no1=$data['card_no1'];
			$fingerprint=str_replace("-", "", input("fingerprint",''));
			model('card')->where("card_no1='$card_no1'")->setField("fingerprint",$fingerprint);
			//云端备份
			aysncronous_out("Api/InterfaceApi/add",
				array(
						'fingerprint'=>$fingerprint,
						'card_no'=>$data['card_no'],
						'start_date'=>input('start_date'),
						'effect_date'=>input('effect_date'),
						'surplus'=>input('surplus'),
						'in_varifycation'=>input('in_varifycation'),
						'out_varifycation'=>input('out_varifycation'),
						'in_surplus_limt'=>input('in_surplus_limt'),
						'in_surplus_sub'=>input('in_surplus_sub'),
						'status'=>input('status'),
						'mobile'=>input('mobile'),
						'id_number'=>$data['card_no'],
						'name'=>input('name'),
						'shop_id'=>config("database.shop_id"),	
				)
			);
			return $this->ajax_success();
		}
	}
	
	/**
	 * 业务接口修改 ba'yuan'
	 * http://tp5.com/public/api/InterfaceApi/update/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c
	 * */
	 public function update(){
	 	 $card_no=input('card_no/s');
		 
		 if(!empty($card_no)){
			$card_no=str_pad($card_no, 10,'0',STR_PAD_LEFT);
		}
		 
		 //卡表需要的数据
		 $card['id']=db('Card')->where("card_no1='$card_no'")->fetchsql(FALSE)->value("id");
		 if(!empty(input("type"))){
		 	 $card['type']=input("type");
		 }
		 if(!empty(input("owner_type"))){
		 	 $card['owner_type']=input("owner_type");
		 }
		 if(!empty(input("fingerprint"))){		 	 		 	 
		 	 $card['fingerprint']=str_replace("-", "", input("fingerprint",''));
		 }
		//////////////////////////////////////////////////////////////////////// 
	 	 //会员接口表需要修改的字段
	 	 $data['id']=db('interface')->where("card_no1='$card_no'")->fetchsql(FALSE)->value("id");
		 if(empty($data['id'])){
		 	return $this->ajax_error('','0004',config('err_code.0004'));
		 }
		 
		 //参数 云端备份
		 $params=db('interface')->find($data['id']);
		 if(!empty(input("fingerprint"))){		 	 		 	 
		 	 $params['fingerprint']=str_replace("-", "", input("fingerprint",''));
		 }else{
		 	 $params['fingerprint']=null;
		 }
		 
		 $flag=TRUE;
		 //需要修改字段的值
		 if(!empty(input('effect_date'))){
		 	$flag=$this->validate(input(''),'InterfaceApi.effect_date');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['effect_date']=input('effect_date');
			$params['effect_date']=input('effect_date');
		 }
		 
		 if(input('surplus')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.surplus');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['surplus']=input('surplus');
			$params['surplus']=input('surplus');
		 }
		 
		 if(input('in_varifycation')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.in_varifycation');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['in_varifycation']=input('in_varifycation');
			$params['in_varifycation']=input('in_varifycation');
		 }
		 
		  if(input('out_varifycation')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.out_varifycation');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
		}
		  if(!empty(input('mobile'))){
		  	$data['mobile']=input('mobile');
			$params['mobile']=input('mobile');
		  }
			
		 if(!empty(input('name'))){
		  	$data['name']=input('name');
			$params['name']=input('name');
		  }	
		 
		  if(input('in_surplus_limt')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.in_surplus_limt');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['in_surplus_limt']=input('in_surplus_limt');
			$params['in_surplus_limt']=input('in_surplus_limt');
		 }
		  
		 if(input('in_surplus_sub')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.in_surplus_sub');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['in_surplus_sub']=input('in_surplus_sub');
			$params['in_surplus_sub']=input('in_surplus_sub');
		 }
		if(input('status')!==null){
			$data['status']=input('status');
			$params['status']=input('status');
		 }
		
		if(input('start_date')!==null){
			$data['start_date']=input('start_date');
			$params['start_date']=input('start_date');
		 }
		
		$card['card_no1']=$card_no;
		$card['card_no']=$this->to_hex_revers($card_no);
		db('card')->update($card);// 修改表
		
		$data['card_no1']=$card_no;
		$data['card_no']=$this->to_hex_revers($card_no);
	 	db('interface')->update($data);
		 
		aysncronous_out("Api/InterfaceApi/update",$this->_get_param_update($params)); 
		return  $this->ajax_success();
	 }
	function _get_param_update($data){
		$result=array(
						'fingerprint'=>$data['fingerprint'],
						'card_no'=>$data['card_no'],
						'start_date'=>$data['start_date'],
						'effect_date'=>$data['effect_date'],
						'surplus'=>$data['surplus'],
						'in_varifycation'=>$data['in_varifycation'],
						'out_varifycation'=>$data['out_varifycation'],
						'in_surplus_limt'=>$data['in_surplus_limt'],
						'in_surplus_sub'=>$data['in_surplus_sub'],
						'status'=>$data['status'],
						'mobile'=>$data['mobile'],
						'id_number'=>$data['card_no'],
						'name'=>$data['name'],
						'shop_id'=>config("database.shop_id"),	
		);
		return $result;
	}
	
	/*批量修改会员状态*/
	function update_batch(){
		$data=htmlspecialchars_decode(input('data'));
		if(empty($data)){
			return $this->ajax_error('','0003',config('err_code.0003'));
		}else{
			$data=json_decode($data,TRUE);
			foreach ($data as $key => $value) {
				 $card_no=$value['card_no'];
				 if(!empty($card_no)){
					$card_no=str_pad($card_no, 10,'0',STR_PAD_LEFT);
				}else{
					continue;
				}
				db("interface")->where("card_no1='$card_no'")->setField(array('status'=>$value['status']));
			}
			return $this->ajax_success();
		}
	}
	
	function test(){
		var_dump($this->to_dec_revers('545d2559'));die;
	}		
}
